"""
重试策略配置
定义重试策略的数据类和工厂函数
"""
from dataclasses import dataclass
from typing import Optional, Type, Dict, Any

@dataclass
class RetryConfig:
	"""重试配置数据类"""
	max_retries: int = 3
	base_delay: float = 1.0
	max_delay: float = 30.0
	jitter: bool = True
	
	@classmethod
	def for_provider(cls, provider_type: str, error_type: Optional[Type[Exception]] = None) -> 'RetryConfig':
		"""根据提供商和错误类型创建配置"""
		# 默认配置
		config = cls()
		
		# 提供商特定配置
		if provider_type == "anthropic":
			config.base_delay = 1.5
		elif provider_type == "openrouter":
			config.base_delay = 2.0
			config.max_delay = 45.0
		elif provider_type == "moonshot":
			config.base_delay = 1.0
		
		# 错误类型特定配置
		if error_type:
			error_name = error_type.__name__
			
			if error_name == "RateLimitError":
				config.max_retries = 5
				config.base_delay = 2.0
				config.max_delay = 120.0
			elif error_name == "ServerError":
				config.max_retries = 4
				config.base_delay = 1.5
			elif error_name in ("ConnectionError", "TimeoutError"):
				config.max_retries = 3
				config.base_delay = 1.0
				config.max_delay = 30.0
			# 不应该重试的错误
			elif error_name in ("AuthenticationError", "QuotaExceededError", 
							 "ConfigurationError", "InvalidRequestError",
							 "ValueError", "TypeError"):
				config.max_retries = 0
				
		return config
